package com.yiren.dbaa.module.project_edit.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiren.backstage.config.enums.ExamUiEnum;
import com.yiren.backstage.config.enums.SiteConfigCodeEnum;
import com.yiren.backstage.currency.constant.SignalGConstant;
import com.yiren.dbaa.R;
import com.yiren.dbaa.util.DensityUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by WANGRONGXIANG
 * on 2020/10/17
 * Describe 考车界面模板适配器
 */
public class ExamTemplateAdapter extends BaseAdapter {

    private Context context;
    private OnExamTempSelectListener listener;
    private ExamUiEnum[] examUis;

    /*** 选择的模板的位置 */
    private int selectPosition;
    /*** 点击图片的位置 */
    private int selectImagePosition;
    private int PX = -1;

    //考车界面图片数组
    private static int[] uiImageView = {
            R.mipmap.exam_dj_interface,
            R.mipmap.exam_njdl_interface,
            R.mipmap.exam_sl_interface,
            R.mipmap.exam_jyzt_interface,
            R.mipmap.exam_jzj_interface,
            R.mipmap.exam_ghgd_interface,
            R.mipmap.exam_bkd_interface,
            R.mipmap.exam_xayd_interface,
            R.mipmap.exam_hy_interface,
            R.mipmap.exam_xahz_interface,
            R.mipmap.exam_hge_interface
    };

    public ExamTemplateAdapter(Context context, OnExamTempSelectListener listener) {
        this.context = context;
        this.listener = listener;
        examUis = ExamUiEnum.values();
        PX = DensityUtil.dip2px(context, 1);
        selectPosition = ExamUiEnum.valueOf((String) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.L1.toString())).getCode();
        selectImagePosition = ExamUiEnum.valueOf((String) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.L1.toString())).getCode();
    }

    @Override
    public int getCount() {
        return examUis.length;
    }

    @Override
    public Object getItem(int position) {
        return examUis[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder mHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_exam_template, parent, false);
            mHolder = new ViewHolder(convertView);
            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }
        mHolder.tvExamTitle.setText(examUis[position].getDescription());
        mHolder.ivExamDesign.setImageResource(uiImageView[position]);
        // 同步使用按钮中的文字
        if (position == selectPosition) {
            mHolder.btnUseTemplate.setText(R.string.wfa_choose_ui_detail_used);
            mHolder.btnUseTemplate.setBackground(context.getResources().getDrawable(R.drawable.shape_dbaf_gread_corner5_soild_bg));
            mHolder.btnUseTemplate.setFocusable(false);
        } else {
            mHolder.btnUseTemplate.setText(R.string.wfa_choose_ui_detail_use1);
            mHolder.btnUseTemplate.setBackground(context.getResources().getDrawable(R.drawable.shape_dbaf_blue_corner5_soild_bg));
            mHolder.btnUseTemplate.setFocusable(true);
        }
        // 同步选择图片后方框颜色
        if (position == selectImagePosition) {
            mHolder.ivExamDesign.setBackgroundResource(R.drawable.setting_interface_blue);
            mHolder.ivExamDesign.setFocusable(false);
        } else {
            mHolder.ivExamDesign.setBackgroundResource(R.drawable.setting_interface_grey);
            mHolder.ivExamDesign.setFocusable(true);
        }
        mHolder.ivExamDesign.setPadding(PX, PX, PX, PX);
        //考试模板
        mHolder.ivExamDesign.setOnClickListener(v -> {
            selectImagePosition = position;
            notifyDataSetChanged();
        });
        //预览
        mHolder.btnPreview.setOnClickListener(v -> {
        });
        //使用模板
        mHolder.btnUseTemplate.setOnClickListener(v -> {
            selectPosition = position;
            selectImagePosition = position;
            // 保存记录到配置文件
            if (listener != null) {
                listener.onExamTempSelect(position);
            }
            notifyDataSetChanged();
        });
        return convertView;
    }

    class ViewHolder {
        @BindView(R.id.tv_exam_title)
        TextView tvExamTitle;
        @BindView(R.id.iv_exam_design)
        ImageView ivExamDesign;
        @BindView(R.id.btn_preview)
        Button btnPreview;
        @BindView(R.id.btn_use_template)
        Button btnUseTemplate;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public interface OnExamTempSelectListener {

        void onExamTempSelect(int examUi);
    }

}
